﻿using BUSINESS;
using BUSINESS.ExcelService;
using DataAccessObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI.LopHoc
{
    /// <summary>
    /// Interaction logic for GUILopHocChuNhiem.xaml
    /// </summary>
    public partial class GUILopHocChuNhiem : Window
    {
        private GIAOVIEN giaovien;
        private LOPHOC lophoc;
        private List<HOCSINH> listHocSinh = new List<HOCSINH>();

        private int _noOfErrors = 0;

        public GUILopHocChuNhiem()
        {
            Initialize();

        }

        public GUILopHocChuNhiem(GIAOVIEN giaovien)
        {
            this.giaovien = giaovien;
            Initialize();      
        }

        private void Initialize()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeControls();
        }

        private void InitializeControls()
        {
            txtNgayNhapHoc.SelectedDate = DateTime.Now;
            txtNgaySinh.SelectedDate = new DateTime(1900, 1, 1);
            ////////
            InitListData();
        }

        private void Validation_Error(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added)
                _noOfErrors++;
            else
                _noOfErrors--;
        }

        private void UpdateState()
        {
            btnCapNhat.IsEnabled = true;
        }

        private void SaveState()
        {
            btnCapNhat.IsEnabled = false;
        }


        private void Add_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _noOfErrors == 0;
            e.Handled = true;
        }

        private void Add_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            update();
        }

        private void tbHocSinh_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateState();
            UpdateData(sender);

        }

        private void setErrorMessage(string message)
        {
            errorAlert.Visibility = Visibility.Hidden;
            if (message == null || message == string.Empty)
            {
                ResetForm();
                SaveState();
                return;
            }
            txtError.Content = message;
            errorAlert.Visibility = Visibility.Visible;
        }

        private void ResetForm()
        {
            txtDiaChi.Text = txtGioiTinh.Text = txtHoTen.Text = txtMaHocSinh.Text = txtQueQuan.Text = string.Empty;
            errorAlert.Visibility = Visibility.Hidden;
        }

        private void update()
        {
            HOCSINH hocsinh = tbHocSinh.SelectedItem as HOCSINH;
            HOCSINH gv = mainGrid.DataContext as HOCSINH;
            hocsinh.copyData(gv);
            string result = HocSinhBUS.updateHocSinh(hocsinh);
            setErrorMessage(result);
        }

        private void UpdateData(object sender)
        {
            HOCSINH HOCSINH = (sender as DataGrid).SelectedItem as HOCSINH;
            setData(HOCSINH);
        }

        private void setData(HOCSINH HOCSINH)
        {
            txtMaHocSinh.Text = HOCSINH.MaHocSinh;
            txtHoTen.Text = HOCSINH.HoTen;
            txtGioiTinh.SelectedIndex = HOCSINH.GioiTinh ? 0 : 1;
            txtDiaChi.Text = HOCSINH.DiaChi;
            txtNgaySinh.SelectedDate = HOCSINH.NgaySinh;
            txtNgayNghiHoc.SelectedDate = HOCSINH.NgayNghiHoc;
            txtNgayNhapHoc.SelectedDate = HOCSINH.NgayNhapHoc;
            txtQueQuan.Text = HOCSINH.QueQuan;
            txtNguoiBaoHo.Text = HOCSINH.NguoiBaoHo;
        }

        private void InitListData()
        {
            tbHocSinh.ItemsSource = listHocSinh;
            ///////////////////////////////////////////////////
            txtInputListNamHoc.ItemsSource = NamHocBUS.getAllNamHoc();
            if (txtInputListNamHoc.Items.Count > 0) txtInputListNamHoc.SelectedIndex = 0;
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void txtInputListNamHoc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var namhoc = txtInputListNamHoc.SelectedItem as NAMHOC;
            if (namhoc == null) return;
            lophoc = LopHocBUS.getLopHocByNamHocAndGiaoVien(namhoc, giaovien);
            listHocSinh.Clear();
            listHocSinh.AddRange(HocSinhBUS.getAllHocSinhByLopHoc(lophoc));
            tbHocSinh.Items.Refresh();
            if (tbHocSinh.Items.Count > 0) tbHocSinh.SelectedIndex = 0;
            txtInputTenLop.Text = lophoc.TenLopHoc;
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            string pathname = "DanhSachHocSinhLop " + lophoc.TenLopHoc;
            HocSinhExcel.WriteToFile(pathname,giaovien, lophoc, listHocSinh);
        }
    }
}
